<?php

class ImageController extends BaseController
{
    private $req;
    private $auth;

    public function init()
    {
        $this->req = $this->getRequest();
        if ($this->req->isXMLHttpRequest()) {
            Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
            $layout = Zend_Layout::getMvcInstance();
            if ($layout instanceof Zend_Layout) {
                $layout->disableLayout();
            }
        }
    }

    /**
     * 公共上传商品图片
     */
    public function uploadImageAction()
    {
        $req = $this->getRequest();
        if ($req->isPost() && $req->isXMLHttpRequest() && $_FILES) {
            if (isset($_FILES['mypic']) && $_FILES['mypic']) {
                $suffix = $_FILES['mypic']['name'];
                $postfix = substr($suffix, -4);
                if ($_FILES["mypic"]['size'] > 2048000) {
                    return Response::Error(0, "图片大小不能超过2M");
                }
                $postfix = strtolower($postfix);
                if ($postfix == '.jpg' || $postfix == '.png' || $postfix == 'jpeg') {
                    $today = rand(100, 999);
                    $pic_name = date("YmdHis") . $today . $postfix;
                    $pic_url = \Util\Image::pcUpload(
                        array('name' => $pic_name,
                            'type' => $_FILES["mypic"]['type'],
                            'tmp_name' => $_FILES["mypic"]['tmp_name'],
                            'error' => $_FILES["mypic"]['error'],
                            'size' => $_FILES["mypic"]['size']
                        )
                    );
                    if ($pic_url == false) {
                        return Response::Error(0, "上传图片出错！");
                    }
                    $arr = array(
                        'name' => $suffix,
                        'pic' => $pic_name,
                        'size' => $_FILES["mypic"]['size']
                    );
                    return Response::Success($arr);
                } else {
                    return Response::Error(0, "图片格式只支持jpg,jpeg,png格式！");
                }
            }
        }

    }

}